package com.base.cn.platform.os.controller.course.cou.record;

import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.redis.RedisChannel;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.FileExportImportUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.common.utils.file.FileDownloadUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.catalog.CourseCatalog;
import com.base.cn.platform.os.entity.course.cou.Course;
import com.base.cn.platform.os.entity.course.cou.buy.CourseBuyRecord;
import com.base.cn.platform.os.entity.course.cou.record.CoursePlayRecord;
import com.base.cn.platform.os.entity.course.cou.record.CoursePlayRecordCondition;
import com.base.cn.platform.os.entity.course.material.CouMaterial;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.service.course.cou.CourseBiz;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordBiz;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordBiz;
import com.base.cn.platform.os.service.course.material.CouMaterialBiz;
import com.base.cn.platform.os.service.exam.record.ExamRecordService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 课程播放记录
 *
 * @author SK
 * @since 2018-06-13
 **/
@RestController
@RequestMapping("/course")
public class CoursePlayRecordController extends BaseController {

    @Autowired
    private CoursePlayRecordBiz coursePlayRecordBiz;
    @Autowired
    private CusUserService cusUserService;
    @Autowired
    private CouMaterialBiz couMaterialBiz;
    @Autowired
    private CourseBiz courseBiz;
    @Autowired
    private CourseBuyRecordBiz courseBuyRecordBiz;
    @Autowired
    private ExamRecordService examRecordService;
    @Autowired
    CustomCacheChannel commonCacheChannel;
    @Autowired
    private SysConfigService sysConfigService;


    /**
     * 保存随堂练习
     *
     * @param examRecordId 随堂练习ID
     * @param courseId     购买的课程/套餐ID
     * @param catalogId    目录
     * @param userId       用户ID
     * @return Map<String   ,   Object>
     */
    @RequestMapping("/saveExercises")
    public Map<String, Object> saveExercises(@RequestParam("examRecordId") BigDecimal examRecordId,
                                             @RequestParam("courseId") BigDecimal courseId,
                                             @RequestParam("catalogId") BigDecimal catalogId,
                                             @RequestParam("userId") BigDecimal userId) {
        Map<String, Object> resultMap = examRecordService.submitExamRecord(userId, examRecordId, 2, 0);
        if (!ResultUtil.getSuccess(resultMap)) {
            return resultMap;
        }
        //随堂练习记录
        Map<String, Object> examRecord = (Map<String, Object>) ResultUtil.getEntity(resultMap);
        int userScore = (int) examRecord.get("userScore");
        //设置播放记录数据
        CoursePlayRecord coursePlayRecord = new CoursePlayRecord();
        coursePlayRecord.setCourseId(courseId);
        coursePlayRecord.setCatalogId(catalogId);
        coursePlayRecord.setUserId(userId);
        coursePlayRecord.setPractice(userScore);
        coursePlayRecordBiz.saveCoursePlayRecord(coursePlayRecord);
        //获取课程对象
        Course course = courseBiz.findById(courseId);
        //节点随堂练习总分
        int score = (int) examRecord.get("score");
        //用户得分率
        double _scorePercent = new Double(coursePlayRecord.getPractice()) / new Double(score) * 100D;
        boolean adopt = false;
        //判断用户是否通过
        if (_scorePercent >= course.getScorePercent()) {
            adopt = true;
        }
        resultMap.put("adopt", adopt);//是否通过
        resultMap.put("scorePercent", course.getScorePercent());//要求通过率
        DecimalFormat df = new DecimalFormat("#.##");
        resultMap.put("userPercent", df.format(_scorePercent));//用户得分率
        return resultMap;
    }

    /**
     * 保存课程播放记录
     *
     * @param coursePlayRecord 课程播放记录
     * @return 保存结果
     */
    @RequestMapping("/saveCoursePlayRecord")
    public Map<String, Object> saveCoursePlayRecord(@RequestBody CoursePlayRecord coursePlayRecord) {
        if (!DataUtil.idIsNotNull(coursePlayRecord.getUserId())) {
            return ResultUtil.ERROR("用户未登录，播放记录保存失败");
        }
        if (!DataUtil.idIsNotNull(coursePlayRecord.getBuyCourseId()) || !DataUtil.idIsNotNull(coursePlayRecord.getCourseId())) {
            return ResultUtil.ERROR("播放记录课程数据错误");
        }
        BigDecimal courseId = coursePlayRecord.getBuyCourseId();
        BigDecimal userId = coursePlayRecord.getUserId();
        CourseBuyRecord courseBuyRecord = new CourseBuyRecord();
        courseBuyRecord.setCourseId(courseId);
        courseBuyRecord.setUserId(userId);
        List<CourseBuyRecord> courseBuyRecordList = courseBuyRecordBiz.findCourseBuyRecordList(courseBuyRecord, false);
        if (!courseBuyRecordList.isEmpty()) {
            Map<String, Object> check = this.checkCoursePlayRecord(coursePlayRecord);
            if (Objects.nonNull(check)) {
                return check;
            }
            coursePlayRecordBiz.saveCoursePlayRecord(coursePlayRecord);
            //添加观看积分
//            cusUserIntegralService.addUserIntegral(ActionKey.COURSE,coursePlayRecord.getUserId(),coursePlayRecord.getCourseId(),0,2);
            return ResultUtil.SUCCESS("保存播放记录成功");
        }
        return ResultUtil.SUCCESS("");
    }

    /**
     * 验证播放记录
     *
     * @param coursePlayRecord
     * @return Map<String   ,       Object>
     */
    private Map<String, Object> checkCoursePlayRecord(CoursePlayRecord coursePlayRecord) {
        if (!DataUtil.idIsNotNull(coursePlayRecord.getCourseId())
                || !DataUtil.idIsNotNull(coursePlayRecord.getCatalogId())
                || !DataUtil.idIsNotNull(coursePlayRecord.getUserId())) {
            return ResultUtil.ERROR("数据课程，播放记录保存失败");
        }
        return null;
    }

    /**
     * 查询课程播放记录列表
     *
     * @param condition  课程播放记录查询条件
     * @param setCourse  设置课程
     * @param setCreator 设置前台用户
     * @return 课程播放记录列表
     */
    @RequestMapping("/findCoursePlayRecordPage")
    public PageInfo<CoursePlayRecord> findCoursePlayRecordPage(@RequestBody CoursePlayRecord condition,
                                                               @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                                                               @RequestParam(value = "setCourse", required = false, defaultValue = "false") boolean setCourse,
                                                               @RequestParam(value = "setCreator", required = false, defaultValue = "false") boolean setCreator,
                                                               @RequestParam(value = "setCatalogData", required = false, defaultValue = "false") boolean setCatalogData,
                                                               @RequestParam(value = "setCatalogTeacher", required = false, defaultValue = "false") boolean setCatalogTeacher) {
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        return coursePlayRecordBiz.findCoursePlayRecordPage(condition, page, setCourse, setCreator, setCatalogData, setCatalogTeacher);
    }

    /**
     * 查询用户学习了多少个课程
     *
     * @param userId 用户ID
     * @return 已学习的课程数据
     */
    @RequestMapping("/queryUserStudyCourseNum")
    public Integer queryUserStudyCourseNum(BigDecimal userId) {
        return coursePlayRecordBiz.queryUserStudyCourseNum(userId);
    }

    /**
     * 查询用户总学习时间
     *
     * @param userId   用户ID
     * @param courseId 课程ID
     * @return 学习时间
     */
    @RequestMapping("/queryUserStudySumTime")
    public Integer queryUserStudySumTime(@RequestParam("userId") BigDecimal userId,
                                         @RequestParam("courseId") BigDecimal courseId) {
        return coursePlayRecordBiz.queryUserStudySumTime(userId, courseId);
    }

    /**
     * 查询用户某一周的学习时间
     *
     * @param userId   用户ID
     * @param courseId 课程ID
     * @return 学习时间
     */
    @RequestMapping("/queryUserWeekStudySumTime")
    public Integer queryUserWeekStudySumTime(@RequestParam("userId") BigDecimal userId,
                                             @RequestParam("courseId") BigDecimal courseId) {
        return coursePlayRecordBiz.queryUserWeekStudySumTime(userId, courseId);
    }

    /**
     * 查询用户对课程的学习的排行名次（根据学习时间进行排序）
     *
     * @param userId   用户ID
     * @param courseId 课程ID
     * @return 排名名字
     */
    @RequestMapping("/queryUserStudyCourseRankings")
    public Integer queryUserStudyCourseRankings(@RequestParam("userId") BigDecimal userId,
                                                @RequestParam("courseId") BigDecimal courseId) {
        return coursePlayRecordBiz.queryUserStudyCourseRankings(userId, courseId);
    }

    /**
     * 查询学习时间排行第一的用户ID
     *
     * @param courseId 课程ID
     * @return 用户ID
     */
    @RequestMapping("/queryUserStudyCourseRankOne")
    public BigDecimal queryUserStudyCourseRankOne(@RequestParam("courseId") BigDecimal courseId) {
        return coursePlayRecordBiz.queryUserStudyCourseRankOne(courseId);
    }

    /**
     * 查询学习时间排行第一的用户对象
     *
     * @param courseId 课程ID
     * @return 用户ID
     */
    @RequestMapping("/queryUserStudyCourseRankOneUser")
    public Map<String, Object> queryUserStudyCourseRankOneUser(@RequestParam("courseId") BigDecimal courseId) {
        BigDecimal userId = coursePlayRecordBiz.queryUserStudyCourseRankOne(courseId);
        if (DataUtil.idIsNotNull(userId)) {
            return cusUserService.userInfo(userId, false, false, false, false, false, false, false);
        }
        return null;
    }

    /**
     * 获取用户最后一个播放的目录节点
     *
     * @param userId 用户ID
     * @return CoursePlayRecord
     */
    @RequestMapping("/getUserLastPlayCourseRecord")
    public CoursePlayRecord getUserLastPlayCourseRecord(@RequestParam("userId") BigDecimal userId) {
        return coursePlayRecordBiz.getUserLastPlayCourseRecord(userId);
    }


    /**
     * 导出学习记录
     *
     * @param condition 查询条件
     * @param pageSize  数据显示数量
     * @return
     */
    @RequestMapping("/downloadExcelDetailsList")
    public ResponseEntity<byte[]> downloadExcelDetailsList(@RequestBody CoursePlayRecord condition,
                                                           @RequestParam(value = "pageSize") int pageSize) throws Exception {
        String[] heads = new String[]{"ID", "观看课程-播放章节", "课程类型", "总播放时间", "播放次数", "学员"};
        String expName = "学习记录";
        //生成文件的容器
        List<Map<String, Object>> srcFile = new ArrayList<>();
        //文件名
        expName = expName + System.currentTimeMillis();

        PageInfo<CoursePlayRecord> coursePlayRecordPage = this.findCoursePlayRecordPage(condition, 1, pageSize, true, true, true, false);

        if (ObjectUtils.isNotEmpty(coursePlayRecordPage.getList())) {
            //每2000数据一个execl，如果页数大于1循环生成
            if (coursePlayRecordPage.getPages() >= 1) {
                for (int i = 1; i <= coursePlayRecordPage.getPages(); i++) {
                    if (i > 1) {
                        coursePlayRecordPage = this.findCoursePlayRecordPage(condition, 1, pageSize, true, true, true, false);
                    }
                    Map<String, Object> files = new HashMap<>();
                    InputStream inputStream = FileExportImportUtil.createExcel(heads, getData(coursePlayRecordPage.getList()));
                    files.put("name", new StringBuilder(expName).append("_").append(i).append(".xls"));
                    files.put("inputStream", inputStream);
                    srcFile.add(files);
                }
            }
        }
        return FileDownloadUtil.createRar(srcFile, expName);
    }

    /**
     * @param orderList 数据
     * @return
     */
    private List<List<String>> getData(List<CoursePlayRecord> orderList) {
        List<List<String>> data = new ArrayList<>();

        orderList.forEach((v) -> {
            Map<String, Object> user = v.getUser();
            Course course = v.getBuyCourse();
            CourseCatalog courseCatalog = v.getCourseCatalog();
            String courseTypeKey = "";
            if (CourseType.COURSE_TYPE_COLUMNS.getKey().equals(courseTypeKey)) {
                courseTypeKey = CourseType.COURSE_TYPE_COLUMNS.getValue();
            } else if (CourseType.COURSE_TYPE_LIVE.getKey().equals(courseTypeKey)) {
                courseTypeKey = CourseType.COURSE_TYPE_LIVE.getValue();
            } else if (CourseType.COURSE_TYPE_VIDEO.getKey().equals(courseTypeKey)) {
                courseTypeKey = CourseType.COURSE_TYPE_VIDEO.getValue();
            } else if (CourseType.COURSE_TYPE_PACKAGE.getKey().equals(courseTypeKey)) {
                courseTypeKey = CourseType.COURSE_TYPE_PACKAGE.getValue();
            }
            String decimal = "0";
            if (ObjectUtils.isNotEmpty(v.getDurationCatalogNum())) {
                DecimalFormat df = new DecimalFormat("#.00");
                decimal = String.format("%.2f", v.getDurationCatalogNum() / 60.0);
            }
            data.add(Arrays.asList(
                    v.getId().toString(),
                    course.getCourseName() + (ObjectUtils.isNotEmpty(courseCatalog) ? courseCatalog.getCatalogName() : ""),
                    courseTypeKey,
                    new StringBuilder(decimal.toString()).append("分钟").toString(),
                    v.getPlayNumber().toString(),
                    ObjectUtils.isNotEmpty(user) ? user.get("nickname").toString() : ""
            ));
        });
        return data;
    }

    /**
     * 查询用户学习列表
     *
     * @param condition
     * @return
     */
    @RequestMapping("/findCoursePlayRecordList")
    public List<CoursePlayRecord> findCoursePlayRecordList(@RequestBody CoursePlayRecordCondition condition) {
        return coursePlayRecordBiz.findCoursePlayRecordList(condition, condition.isCourseData(), condition.isCreatorData());
    }
    //======================================
    /*  *//**
     * 创建用户的学习记录表、学习明细表
     * @param userId 用户ID
     *//*
    @RequestMapping("/createUserPlayRecordTable")
    public void createUserPlayRecordTable(@RequestParam("userId") BigDecimal userId){
        coursePlayRecordBiz.createUserPlayRecordTable(userId);
    }*/

    /**
     *  单课程统计，课程学习人数，完成人数，完课率
     * @param courseId
     * @return
     */
    @RequestMapping("/findCourseStatis")
    public Map<String, Object> findCourseStatis(@RequestParam("courseId") BigDecimal courseId) {
        String format = DateUtils.format(new Date(), "yyyy-MM-dd");
        Map<String,Object>result= (Map<String, Object>) commonCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(), format + courseId + "_COURSE_STATIS").getValue();
        if (ObjectUtils.isEmpty(result)) {
            result=coursePlayRecordBiz.findCourseStatis(courseId);
            commonCacheChannel.set(J2CacheRegion.ONE_DAY_REGION.getKey(), format + courseId + "_COURSE_STATIS", result);
        }
        return result;
    }


    /**
     * 查询课程学习走势统计（学习人数，学习时长）
     * @param dataQueryCondition
     * @return
     */
    @RequestMapping("/queryCourseStudyStatis")
    Map<String, Object> queryCourseStudyStatis(
                                               @RequestParam("courseId")BigDecimal courseId,
                                               @RequestBody StatisticsDateQueryCondition dataQueryCondition){
        dataQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dataQueryCondition.getDayList();
        String key = DateUtils.format(dateList.get(0), "yyyy-MM-dd") + "_" + DateUtils.format(dateList.get(dateList.size()-1), "yyyy-MM-dd") + "_" + courseId+"_STUDY_STATIS";
        Map<String, Object> result =(Map<String,Object>)commonCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(), key).getValue();
        if (ObjectUtils.isEmpty(result)) {
            String timeSql = dataQueryCondition.getTimeSql(dateList, "yyyy-MM-dd");
            result = coursePlayRecordBiz.findCoursePlayRecordByDate(courseId, dateList, timeSql);
            commonCacheChannel.set(J2CacheRegion.ONE_DAY_REGION.getKey(), key, result);
        }

        return ResultUtil.SUCCESS(result);
    }

    /**
     * 课程完成率走势统计（学习人数，学习时长）
     * @param dataQueryCondition
     * @return
     */
    @RequestMapping("/queryCourseFinishStatis")
    Map<String, Object> queryCourseFinishStatis(
                                                @RequestParam("courseId")BigDecimal courseId,
                                                @RequestBody StatisticsDateQueryCondition dataQueryCondition){
        dataQueryCondition.setTimeCondition(new Date());
        List<Date> dateList=dataQueryCondition.getDayList();
        String key = DateUtils.format(dateList.get(0), "yyyy-MM-dd") + "_" + DateUtils.format(dateList.get(dateList.size() - 1), "yyyy-MM-dd") + "_" + courseId + "_FINISH_STATIS";
        Map<String, Object> result =(Map<String,Object>)commonCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(),key ).getValue();
        if (ObjectUtils.isEmpty(result)) {
            String timeSql = dataQueryCondition.getTimeSql(dateList, "yyyy-MM-dd");
            result = coursePlayRecordBiz.queryCourseFinishStatis(courseId, dateList, timeSql);
            commonCacheChannel.set(J2CacheRegion.ONE_DAY_REGION.getKey(), key, result);
        }

        return ResultUtil.SUCCESS(result);
    }

    /**
     * 直播统计
     * @return
     */
    @RequestMapping("/queryLiveStatis")
    public Map<String, Object> queryLiveStatis(
                                               @RequestParam("courseId") BigDecimal courseId,
                                               @RequestParam(value = "materialId",required = false)BigDecimal materialId,
                                               @RequestParam(value = "catalogId",required = false)BigDecimal catalogId) {

        String format = DateUtils.format(new Date(), "yyyy-MM-dd");
        String key=format + courseId+(materialId==null?"":"_"+materialId) + "_LIVE_STATIS";
        Map<String,Object>result= (Map<String, Object>) commonCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(),key ).getValue();
        if (ObjectUtils.isEmpty(result)) {
            result=coursePlayRecordBiz.liveCourseStatis(courseId, materialId,catalogId);
            commonCacheChannel.set(J2CacheRegion.ONE_DAY_REGION.getKey(), key, result);
        }
        return ResultUtil.SUCCESS(result);
    }

    /**
     * @param materialId 素材id
     * @param pageSize   页容量
     * @param pageNum    页码
     * @return
     */
    @RequestMapping("/getInteractivePage")
    public PageInfo<Map<String, Object>> getInteractivePage(
                                                            @RequestParam("materialId") BigDecimal materialId,
                                                            @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        CouMaterial couMaterial = couMaterialBiz.findById(materialId);
        if("CC".equals(couMaterial.getPlayType())){
            return coursePlayRecordBiz.getInteractiveByCCPage(materialId, pageSize, pageNum);
        }else{
            return coursePlayRecordBiz.getInteractiveByTTPage(materialId, pageSize, pageNum);
        }
    }


    /**
     * @param materialId 素材id
     * @param pageSize   页容量
     * @param pageNum    页码
     * @return
     */
    @RequestMapping("/getChatMsgPage")
    public PageInfo<Map<String, Object>> getChatMsgPage(
                                                        @RequestParam("materialId") BigDecimal materialId,
                                                        @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        CouMaterial couMaterial = couMaterialBiz.findById(materialId);
        if("CC".equals(couMaterial.getPlayType())){
            return coursePlayRecordBiz.getChatMsgByCCPage( materialId, pageSize, pageNum);
        }else{
            return coursePlayRecordBiz.getChatMsgByTTPage(materialId, pageSize, pageNum);
        }
    }

    /**
     * @param materialId 素材id
     * @param pageSize   页容量
     * @param pageNum    页码
     * @return
     */
    @RequestMapping("/getRollCallPage")
    public PageInfo<Map<String, Object>> getRollCallPage(
                                                         @RequestParam("materialId") BigDecimal materialId,
                                                         @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        CouMaterial couMaterial = couMaterialBiz.findById(materialId);
        if("CC".equals(couMaterial.getPlayType())){
            return coursePlayRecordBiz.getRollCallByCCPage( materialId, pageSize, pageNum);
        }else{
            return coursePlayRecordBiz.getRollCallByTTPage( materialId, pageSize, pageNum);
        }
    }


    /**
     * 查询课程学习走势统计（学习人数，学习时长）
     * @param dataQueryCondition
     * @return
     */
    @RequestMapping("/queryLiveOnlineStatis")
    Map<String, Object> queryLiveOnlineStatis(
                                              @RequestParam("courseId")BigDecimal courseId,
                                              @RequestBody StatisticsDateQueryCondition dataQueryCondition){
        dataQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dataQueryCondition.getDayList();
        String key = DateUtils.format(dateList.get(0), "yyyy-MM-dd") + "_" + DateUtils.format(dateList.get(dateList.size()-1), "yyyy-MM-dd") + "_" + courseId+"_LiveOnlineStatis";
        Map<String, Object> result =(Map<String,Object>)commonCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(), key).getValue();
        if (ObjectUtils.isEmpty(result)) {
            String timeSql = dataQueryCondition.getTimeSql(dateList, "yyyy-MM-dd");
            result = coursePlayRecordBiz.findLiveOnlineByDate(courseId, dateList, timeSql);
            commonCacheChannel.set(J2CacheRegion.ONE_DAY_REGION.getKey(), key, result);
        }

        return ResultUtil.SUCCESS(result);
    }

    @RequestMapping("/findUserCatalogDuration")
    Integer findUserCatalogDuration(@RequestParam("userId") BigDecimal userId, @RequestParam("courseId") BigDecimal courseId, @RequestParam("catalogId") BigDecimal catalogId) {
        CoursePlayRecord playRecord = coursePlayRecordBiz.findUserCatalogPlayRecord(userId, courseId, courseId, catalogId);
        return ObjectUtils.isNotEmpty(playRecord) ? playRecord.getDuration() : 0;
    }

}
